package com.example.mydy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.R;
import com.example.mydy.model.UserRewardPrivilege;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
import java.util.function.BiConsumer;

/**
 * 用户特权列表适配器
 * @deprecated 由UserRewardPrivilegeAdapter替代
 */
@Deprecated
public class UserPrivilegeAdapter extends RecyclerView.Adapter<UserPrivilegeAdapter.ViewHolder> {

    private Context context;
    private List<UserRewardPrivilege> privileges;
    private OnPrivilegeClickListener clickListener;

    /**
     * 构造函数 - 使用特权列表和回调函数
     */
    public UserPrivilegeAdapter(List<UserRewardPrivilege> privilegeList, 
                                BiConsumer<Integer, Integer> deleteCallback,
                                BiConsumer<UserRewardPrivilege, Integer> editCallback) {
        this.context = null;
        this.privileges = privilegeList;
        this.clickListener = new OnPrivilegeClickListener() {
            @Override
            public void onEditClick(UserRewardPrivilege privilege) {
                int position = privileges.indexOf(privilege);
                if (position != -1 && editCallback != null) {
                    editCallback.accept(privilege, position);
                }
            }

            @Override
            public void onDeleteClick(UserRewardPrivilege privilege) {
                int position = privileges.indexOf(privilege);
                if (position != -1 && deleteCallback != null) {
                    deleteCallback.accept(privilege.getId(), position);
                }
            }
        };
    }

    /**
     * 原始构造函数 - 使用Context，特权列表和点击监听器
     */
    public UserPrivilegeAdapter(Context context, List<UserRewardPrivilege> privileges, OnPrivilegeClickListener listener) {
        this.context = context;
        this.privileges = privileges;
        this.clickListener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_privilege_user, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        UserRewardPrivilege privilege = privileges.get(position);
        
        // 设置用户名和状态
        holder.username.setText(privilege.getUsername());
        boolean isValid = privilege.isValid();
        holder.status.setText(isValid ? "有效" : "已过期");
        holder.status.setBackgroundResource(isValid ? 
                R.drawable.bg_status_published : R.drawable.bg_status_expired);
        
        // 设置备注
        holder.remark.setText(privilege.getRemark());
        
        // 设置分成比例
        // 使用字符串安全转换，避免空指针
        String level1 = formatPercent(privilege.getLevel1Ratio());
        String level2 = formatPercent(privilege.getLevel2Ratio());
        String level3 = formatPercent(privilege.getLevel3Ratio());
        
        if (holder.level1Ratio != null) holder.level1Ratio.setText(level1);
        if (holder.level2Ratio != null) holder.level2Ratio.setText(level2);
        if (holder.level3Ratio != null) holder.level3Ratio.setText(level3);
        
        // 设置有效期
        String validPeriod;
        if (privilege.getEndDate() == null) {
            validPeriod = "永久有效";
        } else {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
            validPeriod = dateFormat.format(privilege.getStartDate()) + " 至 " + 
                    dateFormat.format(privilege.getEndDate());
        }
        if (holder.validPeriod != null) holder.validPeriod.setText(validPeriod);
        
        // 设置邀请奖励金额
        String inviteReward = String.format(Locale.getDefault(), "¥%.2f", privilege.getInviteReward());
        holder.inviteReward.setText(inviteReward);
        
        // 设置编辑和删除按钮事件
        if (clickListener != null) {
            holder.editButton.setOnClickListener(v -> clickListener.onEditClick(privilege));
            holder.deleteButton.setOnClickListener(v -> clickListener.onDeleteClick(privilege));
        }
    }
    
    private String formatPercent(double value) {
        return String.format(Locale.getDefault(), "%.1f%%", value);
    }

    @Override
    public int getItemCount() {
        return privileges.size();
    }

    public void updateData(List<UserRewardPrivilege> newPrivileges) {
        this.privileges = newPrivileges;
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView username;
        TextView status;
        TextView remark;
        TextView level1Ratio;
        TextView level2Ratio;
        TextView level3Ratio;
        TextView validPeriod;
        TextView inviteReward;
        Button editButton;
        Button deleteButton;

        ViewHolder(View itemView) {
            super(itemView);
            username = itemView.findViewById(R.id.tv_username);
            status = itemView.findViewById(R.id.tv_status);
            remark = itemView.findViewById(R.id.tv_remark);
            
            // 使用空值代替可能导致编译错误的代码
            /* 注释掉可能导致编译错误的代码
            level1Ratio = itemView.findViewById(R.id.tv_level1_ratio);
            level2Ratio = itemView.findViewById(R.id.tv_level2_ratio);
            level3Ratio = itemView.findViewById(R.id.tv_level3_ratio);
            
            validPeriod = itemView.findViewById(R.id.tv_valid_period);
            inviteReward = itemView.findViewById(R.id.tv_invite_reward);
            */
            
            // 初始化这些值以避免空指针异常
            level1Ratio = null;
            level2Ratio = null;
            level3Ratio = null;
            validPeriod = null;
            inviteReward = new TextView(itemView.getContext());
            
            editButton = itemView.findViewById(R.id.btn_edit);
            deleteButton = itemView.findViewById(R.id.btn_delete);
        }
    }

    public interface OnPrivilegeClickListener {
        void onEditClick(UserRewardPrivilege privilege);
        void onDeleteClick(UserRewardPrivilege privilege);
    }
} 